package com.example.aiscripteditorserver.controller;


import com.example.aiscripteditorserver.common.tools.excel.ExcelReader;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/command")
public class CommandController {


    /* 内存里放一条待执行指令，生产环境请持久化 */
    private static final AtomicReference<Cmd> CMD = new AtomicReference<>();

    public static void setCommand(Cmd cmd) { CMD.set(cmd); }

    @GetMapping(produces = "application/json")
    public Cmd poll() {
        /* 返回当前指令，取出后清空（一次性） */
        return CMD.getAndSet(null);
    }

    /* 后台人员手动下达指令的调试接口 */
    @PostMapping("/switch/{scriptName}")
    public void switchScript(@PathVariable String scriptName) {
        setCommand(new Cmd("switch", scriptName));
    }

    /* 指令实体 */
    public static class Cmd {
        public String action;   // switch
        public String targetScript; // 脚本名
        public Cmd(String action, String targetScript) {
            this.action = action;
            this.targetScript = targetScript;
        }
    }
    
    @GetMapping("generate_cmd")
    public String generateCmd(@RequestParam String flag) {
        switch (flag) {
            case "监控价格":
                return monitorPrices();
            case "其它":
                return null;
            default: 
                return null;
        }
    }
    
    public String monitorPrices() {
        String cmd = null;
        try {

            File file = new File("F:\\项目相关信息\\25.触控精灵\\价格表.xlsx");
            Map<String, Double> res = ExcelReader.readCol1Col2(file);
            
            StringBuilder sb = new StringBuilder();
            
            res.forEach((k, v) -> {
                String str = String.format("copy \"%s\"", k);
                
            });
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        return cmd;
    }

}
